package com.weizhi.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weizhi.server.dto.CategoryDto;
import com.weizhi.server.dto.CourseDto;
import com.weizhi.server.entity.Category;
import com.weizhi.server.entity.Course;
import com.weizhi.server.entity.CourseContent;
import com.weizhi.server.mapper.CategoryMapper;
import com.weizhi.server.mapper.CourseCategoryMapper;
import com.weizhi.server.mapper.CourseContentMapper;
import com.weizhi.server.mapper.CourseMapper;
import com.weizhi.server.service.CourseCategoryService;
import com.weizhi.server.service.CourseService;
import com.weizhi.server.util.PropertyCopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    private final static Logger logger = LoggerFactory.getLogger(CourseServiceImpl.class);

    @Resource
    private CourseMapper courseMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private CourseCategoryMapper courseCategoryMapper;
    @Resource
    private CourseContentMapper courseContentMapper;
    @Resource
    private CourseCategoryService courseCategoryService;

    @Override
    public PageInfo list(JSONObject param) {
        logger.info(JSONObject.toJSONString(param));
        PageHelper.startPage(param.getInteger("pageIndex"), param.getInteger("pageSize"));
        List<Course> courseList = courseMapper.list();
        PageInfo pageInfo = new PageInfo(courseList);
        List<CourseDto> courseDtoList = PropertyCopyUtil.copyList(courseList, CourseDto.class);
        //根据课程id 查询其分类id
        for(CourseDto courseDto: courseDtoList) {
            List<Long> categoryIdList = courseCategoryMapper.selectByCourseId(courseDto.getId());
            courseDto.setCategoryIdList(categoryIdList);
        }
        pageInfo.setList(courseDtoList);
        return pageInfo;
    }

    @Override
    @Transactional
    public void save(CourseDto courseDto) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDto, course);
        if(course.getId() == null){
            courseMapper.insertSelective(course);
        }else{
            courseMapper.updateByPrimaryKey(course);
            //删除课程分类信息
            courseCategoryMapper.deleteByCourseId(courseDto.getId());
        }
        //批量保存 课程-分类
        courseCategoryService.saveCourseCategory(course.getId(), courseDto.getCategoryIdList());
    }


    @Override
    public void delete(Long id) {
        //删除课程--》删除大章 --》删除小节
        courseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CourseContent getContent(Long id) {
        return courseContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveContent(CourseContent courseContent) {
        int num = courseContentMapper.updateByPrimaryKeyWithBLOBs(courseContent);
        if(num == 0) {
            courseContentMapper.insert(courseContent);
        }
    }
}
